package lew.bing.novels;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 刘国兵 on 2019/8/18.
 *
 * @author 刘国兵
 */
public class H88DUSHUNovelLoader extends JsoupNovelLoader{


    private String catalogueAddress;

    public H88DUSHUNovelLoader(String catalogueAddress) {
        this(3, catalogueAddress);
    }

    public H88DUSHUNovelLoader(int times,String catalogueAddress) {
        super(times, catalogueAddress);
        this.catalogueAddress = catalogueAddress;
    }

    @Override
    protected Elements addressesHref(Document document) {
        return document.select(".mulu>ul>li>a");
    }

    @Override
    protected Novel contentAndTitle(Document document) {
        String title = document.title();
        if (title != null) {
            title = title.replace("- 88读书网", "");
        }
        Elements contentElement = document.select(".yd_text2");
        String content = contentElement.html();

        String htmlContent = contentElement.html();
        if (content != null) {
            String[] split = content.split("<br(\b)*(/)?>");
            StringBuilder builder = new StringBuilder();
            for (String s : split) {
                if (s == null) {
                    continue;
                }
                s = s.replace("&nbsp;","").replace("\n","\r\n").trim();
                if (!"".equals(s)) {
                    builder.append("  ");
                    builder.append(s);
                    builder.append("\r\n");
                    builder.append("\r\n");
                }
            }
            content = builder.toString();
        }
        Charset charset = document.charset();

        Novel novel = new Novel();
        novel.setContent(content);
        novel.setHtmlContent(htmlContent);
        novel.setTitle(title);
        novel.setCharset(charset);
        return novel;
    }


}
